package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysUser;
import com.njym.service.StatisticalAnalysisService;
import com.njym.util.PageUtils;
import com.njym.vo.Json;
import com.njym.vo.StatisticalAnalysisVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @program: StatisticalAnalysisController
 * @description:
 * @author: wb
 * @create: 2018-09-11 16:18
 **/

@PermInfo(value = "统计分析")
@RestController
@RequiresPermissions("a:sa")
@RequestMapping("/statisticalAnalysis")
public class StatisticalAnalysisController {
    private String zero = "0";
    @Autowired
    StatisticalAnalysisService statisticalAnalysisService;

    private static final Logger log = LoggerFactory.getLogger(StatisticalAnalysisController.class);
    @PermInfo("所有汇总")
    @RequiresPermissions("a:sa:staAna")
    @PostMapping("/staAna")
    public Json PlatformBusinesStaAna(@RequestBody String body) throws ParseException {
        String oper = "PlatformBusinessStaAna";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        String orgId = user.getOrgId();

        Object timeInterval = json.get("timeInterval");

        String modeId = json.getString("id");

        Date start = null;
        Date end = null;
        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }

        if(orgId.equals(zero)) {
            orgId = null;
            List<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysis(start, end,orgId,modeId);
            return Json.succ(oper).data("staAna", pfbr);
        }else {
            List<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysis(start, end,orgId,modeId);
            return Json.succ(oper).data("staAna", pfbr);
        }
    }

    @PermInfo("应用汇总")
    @RequiresPermissions("a:sa:staAnaChannel")
    @PostMapping("/staAnaChannel")
    public Json PlatformBusinesStaAnaChannel(@RequestBody String body) {
        String oper = "PlatformBusinessStaAnaChannel";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        String orgId = user.getOrgId();

        Object timeInterval = json.get("timeInterval");

        String channelId = json.getString("id");
        Date start = null;
        Date end = null;
        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        if(zero.equals(orgId)) {
            orgId = null;
            List<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysisChannel(start, end,orgId,channelId);
            return Json.succ(oper).data("staAnaChannel", judge(pfbr,start,end,channelId));
        }else {
            List<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysisChannel(start, end,orgId,channelId);
            return Json.succ(oper).data("staAnaChannel", judge(pfbr,start,end,channelId));
        }
    }

    private List<StatisticalAnalysisVo> judge(List<StatisticalAnalysisVo> list,Date start,Date end,String channelId){
        BigDecimal zeroo = new BigDecimal(0.00);
        for(StatisticalAnalysisVo statisticalAnalysisVo:list){
            if (zeroo.compareTo(statisticalAnalysisVo.getAllTotalFee())==0){
                String totalFee = statisticalAnalysisService.getStatisticalByChannelId(start,end,channelId);
                if(!"0.00".equals(totalFee)){
                    statisticalAnalysisVo.setAllTotalFee(new BigDecimal(totalFee));
                }
            }
        }
        return list;
    }

    @PermInfo("明细汇总")
    @RequiresPermissions("a:sa:staAnaDetail")
    @PostMapping("/staAnaDetail")
    public Json PlatformBusinesStaAnaDetail(@RequestBody String body) {
        String oper = "PlatformBusinessStaAnaDetail";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        String orgId = user.getOrgId();

        Object timeInterval = json.get("timeInterval");
        String modeId = json.getString("pid");
        String channelId = json.getString("aid");
        Date start = null;
        Date end = null;
        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        System.out.println(start);
        System.out.println(end);
        if(orgId.equals(zero)) {
            orgId = null;
            Page<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysisDetail(PageUtils.getPageParam(json),start, end,orgId,modeId,channelId);
            return Json.succ(oper).data("staAnaDetail", pfbr);
        }else {
            Page<StatisticalAnalysisVo> pfbr = statisticalAnalysisService.getStatisticalAnalysisDetail(PageUtils.getPageParam(json),start, end,orgId,modeId,channelId);
            return Json.succ(oper).data("staAnaDetail", pfbr);
        }
    }
}
